Skip to content

Conversation

@saart
Copy link

@saart saart commented Feb 1, 2025

No description provided.

@saart saart mentioned this pull request Feb 1, 2025
@saart saart changed the title support CPU inference - use a pytorch-implementation instead of xformers support CPU inference - use pytorch-implementation when xformers isn't installed Feb 1, 2025
)

def memory_efficient_attention(query, key, value, p):
""" This code is taken from https://facebookresearch.github.io/xformers/components/ops.html """

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
A simplified PyTorch implementation of memory-efficient attention.
Assumes inputs are already reshaped to 4D [batch, heads, seq_len, head_dim].
This implementation is SLOWER and LESS MEMORY-EFFICIENT than xformers,
and does NOT SUPPORT attn_bias.

    Args:
        query (Tensor): shape (batch, heads, seq_len, head_dim)
        key (Tensor): shape (batch, heads, seq_len, head_dim)
        value (Tensor): shape (batch, heads, seq_len, head_dim)
        p (float): dropout probability. Default: 0.0 (no dropout)
        attn_bias:  NOT SUPPORTED IN THIS FALLBACK.
        scale (float, optional): scaling factor for the dot product. If None,
            defaults to 1 / sqrt(head_dim).
    Returns:
        Tensor: shape (batch, heads, seq_len, head_dim)
    """
    
    Its batter to replace the comment with this, it will improve the developer experience.

@xiao-li-econ
Copy link

In the latest version the line from xformers.ops import memory_efficient_attention is in def forward. Need to scroll down and comment it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants